/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.pbp.core.paging;

import ac.pbp.core.FilterableDao;
import ac.pbp.core.filter.FilterCriteria;
import java.util.List;

/**
 *
 * @author Kim
 */
public class FilterAndPaging {

    public static synchronized List loadAll(FilterableDao dao, FilterCriteria filterCriteria) {
        long totalData = 0;
        long totalPage = 1;
        long nextPage = 1;
        long prevPage = 1;
        long startIndex = (filterCriteria.getPage() - 1) * filterCriteria.getMax();
        totalData = dao.getMax();

        if (totalData > 0) {
            if (totalData % filterCriteria.getMax() == 0) {
                totalPage = totalData / filterCriteria.getMax();
            } else {
                long sisa = totalData % filterCriteria.getMax();
                totalPage = ((totalData - sisa) / filterCriteria.getMax()) + 1;
            }

            if (filterCriteria.getPage() - 1 > 0) {
                prevPage = filterCriteria.getPage() - 1;
            }

            if ((filterCriteria.getPage() + 1) <= totalPage) {
                nextPage = filterCriteria.getPage() + 1;
            } else {
                nextPage = totalPage;
            }
        }

        filterCriteria.setNextPage(nextPage);
        filterCriteria.setPrevPage(prevPage);
        filterCriteria.setTotalPage(totalPage);

        return dao.loadAll(startIndex, filterCriteria.getMax());
    }

    public static synchronized List filter(FilterableDao dao, FilterCriteria filterCriteria) {
        long totalData = 0;
        long totalPage = 1;
        long nextPage = 1;
        long prevPage = 1;

        totalData = dao.getMax(filterCriteria);
        System.out.println("totalData : " + totalData);

        if (totalData > 0) {
           
            if (totalData % filterCriteria.getMax() == 0) {
                totalPage = totalData / filterCriteria.getMax();
            } else {
                long sisa = totalData % filterCriteria.getMax();
                totalPage = ((totalData - sisa) / filterCriteria.getMax()) + 1;
            }

            if (filterCriteria.getPage() - 1 > 0) {
                prevPage = filterCriteria.getPage() - 1;
            }

            if ((filterCriteria.getPage() + 1) <= totalPage) {
                nextPage = filterCriteria.getPage() + 1;
            } else {
                nextPage = totalPage;
            }
        }

        filterCriteria.setNextPage(nextPage);
        filterCriteria.setPrevPage(prevPage);
        filterCriteria.setTotalPage(totalPage);

        System.out.println("filter pass man !!!");
        System.out.println(filterCriteria.getMax());
        System.out.println(filterCriteria.getNextPage());
        System.out.println(filterCriteria.getPage());
        System.out.println(filterCriteria.getPrevPage());
        System.out.println(filterCriteria.getTotalPage());
        return dao.filter(filterCriteria);
    }
}
